import DataType from "@/utils/DataType";

const ArraySort = {
	compare(order = "asc") {
		return (a, b) => {
			return order === "desc" ? b - a : a - b;
		};
	},

	compareValues(key, order = "asc") {
		return (a, b) => {
			if (!a.hasOwnProperty(key) || !b.hasOwnProperty(key)) {// 该属性在任何一个对象上都不存在
				return 0;
			}
			const varA = (DataType.isString(a[key])) ? a[key].toUpperCase() : a[key];
			const varB = (DataType.isString(b[key])) ? b[key].toUpperCase() : b[key];
			let comparison = varA > varB ? 1 : -1;
			return ((order === "desc") ? (comparison * -1) : comparison);
		};
	},

	compareString(key, order = "asc") {
		return (a, b) => {
			if (!a.hasOwnProperty(key) || !b.hasOwnProperty(key)) {// 该属性在任何一个对象上都不存在
				return 0;
			}
			const comparison = a[key].localeCompare(b[key]);
			return ((order === "desc") ? (comparison * -1) : comparison);
		};
	}
};
export default ArraySort;
